package com.swak.monitor.dubbo.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.swak.annotation.Delete;
import com.swak.annotation.Get;
import com.swak.annotation.Post;
import com.swak.annotation.Put;
import com.swak.annotation.RestApi;
import com.swak.monitor.dubbo.exception.ParamValidationException;
import com.swak.monitor.dubbo.exception.ResourceNotFoundException;
import com.swak.monitor.dubbo.model.dto.WeightDTO;
import com.swak.monitor.dubbo.service.OverrideService;
import com.swak.monitor.dubbo.utils.Constants;
import com.swak.utils.StringUtils;

@RestApi(value = "DubboWeightApi", path = "/api/monitor/:env/rules/weight")
public class WeightApi {

	@Autowired
	private OverrideService overrideService;
	// @Autowired
	// private ProviderService providerService;

	@Post
	public boolean createWeight(WeightDTO weightDTO, String env) {
		if (StringUtils.isBlank(weightDTO.getService()) && StringUtils.isBlank(weightDTO.getApplication())) {
			throw new ParamValidationException("Either Service or application is required.");
		}
		// String application = weightDTO.getApplication();
//        if (StringUtils.isNotEmpty(application)
//            && this.providerService.findVersionInApplication(application).equals("2.6")) {
//            throw new VersionValidationException("dubbo 2.6 does not support application scope blackwhite list config");
//        }
		overrideService.saveWeight(weightDTO);
		return true;
	}

	@Put(value = "/:id")
	public boolean updateWeight(String id, WeightDTO weightDTO, String env) {
		if (id == null) {
			throw new ParamValidationException("Unknown ID!");
		}
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		WeightDTO weight = overrideService.findWeight(id);
		if (weight == null) {
			throw new ResourceNotFoundException("Unknown ID!");
		}
		overrideService.updateWeight(weightDTO);
		return true;
	}

	@Get
	public List<WeightDTO> searchWeight(String service, String application, String env) {
		if (StringUtils.isBlank(service) && StringUtils.isBlank(application)) {
			throw new ParamValidationException("Either service or application is required");
		}
		WeightDTO weightDTO;
		if (StringUtils.isNotBlank(application)) {
			weightDTO = overrideService.findWeight(application);
		} else {
			weightDTO = overrideService.findWeight(service);
		}
		List<WeightDTO> weightDTOS = new ArrayList<>();
		if (weightDTO != null) {
			weightDTOS.add(weightDTO);
		}
		return weightDTOS;
	}

	@Get
	public WeightDTO detailWeight(String id, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		WeightDTO weightDTO = overrideService.findWeight(id);
		if (weightDTO == null) {
			throw new ResourceNotFoundException("Unknown ID!");
		}
		return weightDTO;
	}

	@Delete(value = "/:id")
	public boolean deleteWeight(String id, String env) {
		id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
		overrideService.deleteWeight(id);
		return true;
	}
}
